package cn.com.blackboard.SISIntergrationBiz.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import blackboard.data.course.Course;
import cn.com.blackboard.DataModule4B2.BBDataUtils.CourseUtil;
import cn.com.blackboard.DataModule4B2.BBDataUtils.GradeExportUtil;
import cn.com.blackboard.DataModule4B2.BBDataUtils.LocaleUtil;
import cn.com.blackboard.DataModule4B2.model.CreditResult;
import cn.com.blackboard.DataModule4B2.service.CreditService;
import cn.com.blackboard.SISIntergrationBiz.model.CourseCreditExportResult;
  
@Controller
@RequestMapping("/exportCreditresult")
public class ExportCreditController {  
	
	private static final Logger logger = LoggerFactory.getLogger(ExportCreditController.class);
    
	private static List<CourseCreditExportResult> result = new ArrayList<CourseCreditExportResult>();
	
	@Autowired 
	CreditService creditService;
	
    @RequestMapping("/showExportResult")  
    public String showExport(ModelMap modelMap) throws IOException{	
    	logger.info("into showExportResult controller");
    	modelMap.addAttribute("result", ExportCreditController.result);
    	Locale locale = LocaleUtil.getLocale();
		modelMap.addAttribute("locale", locale.toString());
		return "/export/showCreditResult"; 
    }
    
    @RequestMapping(value = "exportAll", method = {RequestMethod.POST },produces={"text/html;charset=UTF-8;","application/json;"})
    @ResponseBody
    public String exportAll(ModelMap modelMap) throws IOException{	
    	logger.info("into exportAll controller");
    	List<Course> list = CourseUtil.getAllCourseFromBbPlatform();
    	
    	Locale locale = LocaleUtil.getLocale();
		modelMap.addAttribute("locale", locale.toString());
    	
    	for(Course c:list){
    		List<CreditResult> crList = GradeExportUtil.getResult(c.getId().toString());
    		CourseCreditExportResult ccer = new CourseCreditExportResult();
    		ccer.setCourseId(c.getCourseId());
    		boolean status = creditService.importCredit(crList);
    		if(status)
    			ccer.setExportStatus("导出成绩成功");
    		else
    			ccer.setExportStatus("导出成绩部分失败或有误");
    		ExportCreditController.result.add(ccer);
    	}  	
		return ""; 
    }

}  
